<?php

/*
Copyright:

   This package is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 dated June, 1991.

   This package is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this package; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   02111-1307, USA.

On Debian GNU/Linux systems, the complete text of the GNU General
Public License can be found in `/usr/share/common-licenses/GPL'.

*/
$version = '1.3';

ncurses_init();
ncurses_start_color();
ncurses_init_pair(1,NCURSES_COLOR_RED,NCURSES_COLOR_BLACK);
ncurses_init_pair(2,NCURSES_COLOR_GREEN,NCURSES_COLOR_BLACK);
ncurses_init_pair(3,NCURSES_COLOR_YELLOW,NCURSES_COLOR_BLACK);
ncurses_init_pair(4,NCURSES_COLOR_BLUE,NCURSES_COLOR_BLACK);
ncurses_init_pair(5,NCURSES_COLOR_MAGENTA,NCURSES_COLOR_BLACK);
ncurses_init_pair(6,NCURSES_COLOR_CYAN,NCURSES_COLOR_BLACK);
ncurses_init_pair(7,NCURSES_COLOR_WHITE,NCURSES_COLOR_BLACK);


$f1a = $f1b = $f1c = $f1d = 	array(array(1,2),array(3,4));
$f2a = $f2c = 	array(array(1,1,0),array(0,1,1));
$f2b = $f2d = 	array(array(0,1),array(1,1),array(1,0));
$f3a = $f3c= 	array(array(0,1,1),array(1,1,0));
$f3b = $f3d= 	array(array(1,0),array(1,1),array(0,1));
$f4a = $f4c= 	array(array(1,1,1,1));
$f4b = $f4d= 	array(array(1),array(1),array(1),array(1));
$f5a = array(array(1,1,1),array(1,0,0));
$f5b = array(array(1,1),array(0,1),array(0,1));
$f5c = array(array(0,0,1),array(1,1,1));
$f5d = array(array(1,0),array(1,0),array(1,1));
$f6a = array(array(1,1,1),array(0,0,1));
$f6b = array(array(0,1),array(0,1),array(1,1));
$f6c = array(array(1,0,0),array(1,1,1));
$f6d = array(array(1,1),array(1,0),array(1,0));
$f7a = array(array(1,0),array(1,1),array(1,0));
$f7b = array(array(1,1,1),array(0,1,0));
$f7c = array(array(0,1),array(1,1),array(0,1));
$f7d = array(array(0,1,0),array(1,1,1));

$lstobj = array( 'f1a', 'f1b', 'f1c', 'f1d', 'f2a', 'f2b', 'f2c', 'f2d', 'f3a', 'f3b', 'f3c', 'f3d', 'f4a', 'f4b', 'f4c', 'f4d', 'f5a', 'f5b', 'f5c', 'f5d', 'f6a', 'f6b', 'f6c', 'f6d', 'f7a', 'f7b', 'f7c', 'f7d');
$lstcol = array(NCURSES_COLOR_RED,NCURSES_COLOR_GREEN,NCURSES_COLOR_YELLOW,NCURSES_COLOR_BLUE,NCURSES_COLOR_MAGENTA,NCURSES_COLOR_CYAN);

$stdin = fopen('php://stdin', 'r');

ncurses_getmaxyx(STDSCR, $H, $W);
if($H > 25)
	$H = 25;
if($W > 80)
	$W = 83;
while($W/4 != intval($W/4))
	$W--;

$wdwin = intval($W/2);
$wgwin = $W - $wdwin;

$gwin = ncurses_newwin($H, $wgwin, 0, 0);
ncurses_wborder($gwin, 0, 0, 0, 0, 0, 0, 0, 0); 
$dwin = ncurses_newwin($H, $wdwin, 0, $wgwin);
ncurses_wborder($dwin, 0, 0, 0, 0, 0, 0, 0, 0); 
ncurses_wrefresh($dwin);

function draw_obj($x,$y,$o, $s, $c, $col, $d = 0)
{
 	global $gwin, $dwin, $f1a, $f1b, $f1c, $f1d, $f2a, $f2b, $f2c, $f2d, $f3a, $f3b, $f3c, $f3d, $f4a, $f4b, $f4c, $f4d, $f5a, $f5b, $f5c, $f5d, $f6a, $f6b, $f6c, $f6d, $f7a, $f7b, $f7c, $f7d;
	$ox = $x;
	$obj = "f$o$s";
	$obj = $$obj;
	if($c == 1)
	{
		if($d == 0)
		{
			ncurses_wcolor_set($gwin, $col);
			ncurses_wattron($gwin, NCURSES_A_REVERSE);
		}
		else
		{
			ncurses_wcolor_set($dwin, $col);
			ncurses_wattron($dwin, NCURSES_A_REVERSE);
		}

	}
	foreach($obj as $line)
	{
		foreach($line as $row)
		{
			if($row != 0)
			{
				if($d == 0)
					ncurses_mvwaddstr($gwin, $y, $x, '  ');
				else
					ncurses_mvwaddstr($dwin, $y, $x, '  ');
			}
			$x++;
			$x++;
		}
		$x = $ox;
		$y++;
	}
	if($c == 1)
	{
		if($d == 0)
		{
			ncurses_wcolor_set($gwin, NCURSES_COLOR_WHITE);
			ncurses_wattroff($gwin, NCURSES_A_REVERSE);
		}
		else
		{
			ncurses_wcolor_set($dwin, NCURSES_COLOR_WHITE);
			ncurses_wattroff($dwin, NCURSES_A_REVERSE);
		}
	}
}


function draw_intab($x,$y,$o, $s, &$tab, $col)
{
 	global $gwin, $dwin, $f1a, $f1b, $f1c, $f1d, $f2a, $f2b, $f2c, $f2d, $f3a, $f3b, $f3c, $f3d, $f4a, $f4b, $f4c, $f4d, $f5a, $f5b, $f5c, $f5d, $f6a, $f6b, $f6c, $f6d, $f7a, $f7b, $f7c, $f7d;
        $obj = "f$o$s";
        $obj = $$obj;
	$ox = $x;
        foreach($obj as $line)
        {
                foreach($line as $row)
                {
                        if($row != 0)
			{
                                $tab[$y][$x] = $col;
                                $tab[$y][$x+1] = $col;
			}
                        $x++;
                        $x++;
                }
		$x = $ox;
                $y++;
        }
}


function test_tab($x,$y,$o, $s, &$tab)
{
 	global $gwin, $dwin, $f1a, $f1b, $f1c, $f1d, $f2a, $f2b, $f2c, $f2d, $f3a, $f3b, $f3c, $f3d, $f4a, $f4b, $f4c, $f4d, $f5a, $f5b, $f5c, $f5d, $f6a, $f6b, $f6c, $f6d, $f7a, $f7b, $f7c, $f7d;
        $obj = "f$o$s";
        $obj = $$obj;
        $ox = $x;
        foreach($obj as $line)
        {
                foreach($line as $row)
                {
                        if($row != 0)
                        {
				if($tab[$y][$x] != 0 || $tab[$y][$x+1] != 0)
					return 0;
				
                        }
                        $x++;
                        $x++;
                }
                $x = $ox;
                $y++;
        }
	return 1;
}


function purge_tab(&$tab)
{
	global $H, $score, $level, $msg;
	$msg = '';
	$h = $H-2;
	$z = 0;
	for($k=0;$k<4;$k++)
	{
		for($i=$h;$i>0;$i--)
		{
			if(!ereg('0',implode('',$tab[$i])))
			{
				for($j=$i;$j>0;$j--)
					$tab[$j] = $tab[$j-1];
				$score += (1000 * $level);
				$msg .= "1 line  : bonus + 1000\n";
				$z++;
			}
		}
	}
	if($z == 2)
	{
		$msg .= "2 lines : bonus + 5000\n";
		$score += 5000;
	}
	if($z == 3)
	{
		$msg .= "3 lines : bonus + 15000\n";
		$score += 15000;
	}
	if($z == 4)
	{
		$msg .= "4 lines : bonus score X2\n";
		$score *= 2;
	}
}


$n = 1;
for($i=0;$i<$H;$i++)
{
	$tab[$i] = array();
	for($j=1;$j<($wgwin-1);$j++)
	{
		$tab[$i][$j] = 0;
	}
}
$stime = $rtime = $otime = 1000;
$np = 1;
$pause = 0;
$lmsg = 0;
$score = 0;
$level = 1;
shuffle($lstobj);
$nobj = $lstobj[1];
$ncol = $lstcol[1];
while(1)
{
	$c = '';
	$time = 0;
	while($c == '')
	{
		if(stream_select($read = array($stdin), $write = NULL, $except = NULL, 0))
			$c = ord(fgetc($stdin));
		else
			usleep(1);
		$time++;
		if($time > $stime / 50)
		{
			$time = 0;
			$c = 66;
		}
	}
//$c = 66;

	if($n == 1)
	{
		if($np == 0 && $y < 3)
		{
			$swin = ncurses_newwin(5, $wgwin, intval($H/2)-2, intval($W/4));
	                ncurses_mvwaddstr($swin, 2, 2, 'Score : '.number_format($score));
			ncurses_wborder($swin, 0, 0, 0, 0, 0, 0, 0, 0); 
	        	ncurses_wrefresh($swin);
			sleep(10);
			break;
		}
		$score += (10 * $level);
		$np = 0;
		shuffle($lstobj);
		shuffle($lstcol);
		$y = 1;
		$x = intval($wgwin/2);
		$x++;
		$obj = $nobj;
		$col = $ncol;
		$ncol = $lstcol[1];
		draw_obj( intval($wdwin/2)-2, 12, $nobj[1], $nobj[2], 0, $ncol, 1);
		$nobj = $lstobj[1];
		draw_obj( intval($wdwin/2)-2, 12, $nobj[1], $nobj[2], 1, $ncol, 1);
		$pobj = $$obj;
		purge_tab($tab);

//		$stime--;
		$stime = $stime - intval($stime / 100 );

		$level = intval((($otime/10) - intval($stime / 10)) / 10);

		ncurses_wclear($gwin);
		ncurses_wborder($gwin, 0, 0, 0, 0, 0, 0, 0, 0); 
	        ncurses_wcolor_set($gwin, NCURSES_COLOR_WHITE);
	        ncurses_wattron($gwin, NCURSES_A_REVERSE);
	        foreach($tab as $linen => $line)
	        {
	                foreach($line as $colsn => $cols)
	                {
	                        if($cols != 0)
				{
	        			ncurses_wcolor_set($gwin, $cols);
	                                ncurses_mvwaddstr($gwin, $linen, $colsn, ' ');
	        			ncurses_wcolor_set($gwin, NCURSES_COLOR_WHITE);
				}
	                }
	        }
	        ncurses_wrefresh($gwin);
	        ncurses_wattroff($gwin, NCURSES_A_REVERSE);
		for($u=0;$u<$lmsg;$u++)
			ncurses_mvwaddstr($dwin, 17+$u, 2, '                          ');
		if($msg != "")
		{
	        	$msg = split("\n",$msg);
       			for($m=0;$m < count($msg) ; $m++)
        		{
        		        ncurses_mvwaddstr($dwin, 17+$m, 2, $msg[$m]);
      	 		}
        		ncurses_wrefresh($dwin);
			$lmsg = count($msg);
			$msg = "";
		}

	}
	else
		draw_obj( $x, $y, $obj[1], $obj[2], 0, $col);
	$n = 0;
	switch($c)
	{
		case 112: // pause
			if($pause)
				$pause = 0;
			else
				$pause = 1;
		break;
		case 65: // h
			if($obj[2] == 'a' && !$pause)
				$obj = $obj[0].$obj[1].'b';
			elseif($obj[2] == 'b' && !$pause)
				$obj = $obj[0].$obj[1].'c';
			elseif($obj[2] == 'c' && !$pause)
				$obj = $obj[0].$obj[1].'d';
			elseif($obj[2] == 'd' && !$pause)
				$obj = $obj[0].$obj[1].'a';
			$pobj = $$obj;
		break;
		case 66: // b
			if($y < ($H - count($pobj) - 1) && test_tab($x,$y+1,$obj[1], $obj[2], $tab) && !$pause)
				$y++;
			else
			{
				if(!$pause)
				{
					$n = 1;
					draw_intab($x,$y,$obj[1], $obj[2], $tab,$col);
				}
			}
		break;
		case 68: // g
			if($x > 1 && test_tab($x-1,$y,$obj[1], $obj[2], $tab) && !$pause)
				$x -= 2;
		break;
		case 67: // d
			if($x < ($wgwin - (count($pobj[0]) * 2) - 1) && test_tab($x+1,$y,$obj[1], $obj[2], $tab) && !$pause)
				$x += 2;
		break;
		case 113: // q
		break 2;
		default:
		break;
	}
	draw_obj( $x, $y, $obj[1], $obj[2], 1, $col);
	ncurses_mvwaddstr($gwin, 0, 1, '');
	//ncurses_mvwaddstr($gwin, 0, 1, ' x:'.$x.' y:'.$y.' w:'.$W.' '.$c.'   ');
	ncurses_wrefresh($gwin);
	ncurses_mvwaddstr($dwin, 1, 2, 'PHPnTetris v'.$version);
	ncurses_mvwaddstr($dwin, 3, 2, '(p)               pause');
	ncurses_mvwaddstr($dwin, 4, 2, '(up)              rotate object');
	ncurses_mvwaddstr($dwin, 5, 2, '(down/left/right) move object  ');
	ncurses_mvwaddstr($dwin, 6, 2, '(q)               exit  ');
	ncurses_mvwaddstr($dwin, 9, 2, 'Score : '.$score.'     ');
	ncurses_mvwaddstr($dwin, 10, 2, 'Level : '.$level.'     ');
	//ncurses_mvwaddstr($dwin, 15, 2, 'rtime : '.$rtime.'     ');
	//ncurses_mvwaddstr($dwin, 16, 2, 'stime : '.$stime.'     ');
	ncurses_wrefresh($dwin);

}

ncurses_end();
//echo "\nScore : $score\n";


